package graph.group.zhangtao.guider.getter;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.LinkedList;
import java.util.List;

import graph.group.zhangtao.guider.NavigationActivity;
import graph.group.zhangtao.guider.utils.MySettingsAndTools;

/**
 * 获取index.json,并加载列表
 * Created by Me on 2017/12/24.
 */

public class MainIndexJsonGetter extends AsyncTask {
    private ListView listView;
    private String path;
    private String result;
    private Context context;

    private List<String> wrong;

    public MainIndexJsonGetter(ListView listView, String path, Context context) {
        this.listView = listView;
        this.path = path;
        this.context = context;
        wrong = new LinkedList<>();
        wrong.add("错误，获取失败！请检查网络,点击重试。");
    }

    @Override
    protected Object doInBackground(Object[] objects) {
        try {
            result = MySettingsAndTools.getHtml(path);
        } catch (Exception e) {
            e.printStackTrace();
            result = "wrong";
        }
        return result;
    }

    @Override
    protected void onPostExecute(Object o) {
        Log.e("tao", result);
        if (listView != null) {
            if (result.equals("wrong")) {
                //错误
                updateList(wrong);
            } else {
                //刷新列表
                try {
                    JSONArray jsonArray = new JSONArray(result);
                    final List<String> lists = new LinkedList<>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        String temp = jsonArray.getString(i);
                        lists.add(temp);
                    }
                    updateList(lists);
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            String name = lists.get(i);
                            Intent intent = new Intent(context, NavigationActivity.class);
                            intent.putExtra("id",name);
                            context.startActivity(intent);
//                            Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                    //错误
                    Log.d("tao", "wrong");
                    updateList(wrong);
                }
            }
        }
    }

    private void updateList(List<String> lists) {
        final Object[] strs = lists.toArray();
        listView.setAdapter(new ArrayAdapter<>(context,
                android.R.layout.simple_list_item_1, strs));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                new MainIndexJsonGetter(listView,path,context).execute();
            }
        });
    }

}
